﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using QSHC.QSchedule.Web.Models;
using QSHC.QSchedule.Report;
using System.Globalization;

namespace QSHC.QSchedule.Web.HIS.QSchedule
{
    public partial class Print : System.Web.UI.Page
    {
        private DateTime? _appointmentDate;
        private int? _location;
        private string _physician;
        private string _searchText;
        protected void Page_Load(object sender, EventArgs e)
        {
            //Response.Write(Request.QueryString["LocationID"]);
            //Response.Write(Request.QueryString["Physician"]);
            
            //Response.Write();
          
            //DateTimeFormatInfo dtfi = new DateTimeFormatInfo();
            //dtfi.ShortDatePattern = "dd/MM/yyyy";
            //DateTime objDate = Convert.ToDateTime(dateStr, dtfi);

            //var date = DateTime.Parse(dateStr, dtfi);

            if (!IsPostBack)
            {
                var qschedule = new QScheduleEntities();
                var qshc = new QSchedule_EmployeeEntities();

                var qry = qschedule.Appointments.AsQueryable();

                if (!string.IsNullOrEmpty(Request.QueryString["SearchText"]))
                {
                    string searchText = Request.QueryString["SearchText"];
                    qry = qry.Where(p => p.AppointedPatient.FirstName.Contains(searchText) ||
                                         p.AppointedPatient.LastName.Contains(searchText) ||
                                         p.AppointedPatient.HN.Contains(searchText));
                }

                if (!string.IsNullOrEmpty(Request.QueryString["AppointmentDate"]))
                {
                    var dateStr = Request.QueryString["AppointmentDate"];
                    var appointmentDate = DateTime.ParseExact(dateStr, "ddMMyyyy", null);

                    qry = qry.Where(p => p.AppointmentDate == appointmentDate);
                }

                if (!string.IsNullOrEmpty(Request.QueryString["ServiceID"]))
                {
                    int serviceID = Convert.ToInt32(Request.QueryString["ServiceID"]);
                    qry = qry.Where(p => p.ServiceID == serviceID);
                    
                }

                if (!string.IsNullOrEmpty(Request.QueryString["Physician"]))
                {
                    string physician = Request.QueryString["Physician"];
                    qry = qry.Where(p => p.Physician == physician);
                }
                
                var data = (from a in qry
                            where a.IsDeleted == false
                            orderby a.HN
                            select new QSHC.QSchedule.Report.Models.Appointment
                            {
                                AppointmentID = a.AppointmentID,
                                HN = a.HN,
                                FisrtName = a.AppointedPatient.Title + a.AppointedPatient.FirstName.Trim() + " " + a.AppointedPatient.LastName,
                                LastName = a.AppointedPatient.LastName,
                                AppointmentDate = a.AppointmentDate,
                                LocationID = a.ServiceID,
                                LocationName = a.Service.Name,
                                Physician = a.Physician,
                                EmployeeCode = a.Logs.OrderByDescending(p => p.LogDateTime).FirstOrDefault().EmployeeCode,
                                AppointmentTime = a.AppointmentTime,
                                PaymentName = a.AppointedPatient.Payor,
                            }).ToList();

                foreach (var item in data)
                {
                    var physician = qshc.Employees.Single(p => p.EmployeeCode == item.Physician);
                    var employee = qshc.Employees.Single(p => p.EmployeeCode == item.EmployeeCode);
                    item.PhysicianName = string.Format("{0}{1}  {2}", physician.Title.Name, physician.FirstName, physician.LastName);
                    item.LogName = string.Format("{0}{1}  {2}", employee.Title.Name, employee.FirstName, employee.LastName);
                    
                }

                ReportManager.PrintReport(PrintReportViewer, data);

            }
        }
    }
}